package com.yitong.profile.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yitong.base.controller.BaseController;
import com.yitong.base.dao.api.Login;
import com.yitong.base.domain.Page;
import com.yitong.base.util.Constant;
import com.yitong.profile.domain.TabRight;
import com.yitong.profile.domain.TabUser;
import com.yitong.profile.domain.User;
import com.yitong.profile.service.UserService;

@Controller
public class UserController extends BaseController{
	
	private Logger log = Logger.getLogger(UserController.class);
	@Inject
	private Login login;
	@Inject
	private UserService userService;
	
	private Page<?> page;
	
	@RequestMapping("userAction")
	public String userAction(){
		Map<String, Object> result = new HashMap<String, Object>();
		String target = "menu/null";
		Map<String, Object> param = this.getParameters();//从页面传入的参数
		String toResult = (String) param.get("toResult");
		/** 查询客户信息 */
		if("searchUser".equals(toResult)){
			try {
				List<TabRight> righSubtList = login.getRightSub(loginUser().getUserId(), Integer.parseInt(param.get("rightId").toString()), request);
				result.put("righSubtList", righSubtList);
				int pageNum = Integer.parseInt(param.get("pageNum") == null ? "0" : param.get("pageNum").toString());
				int pageSize = param.get("pageSize") == null ? Constant.DEFAULT_PAGE_SIZE : Integer.parseInt(param.get("pageSize").toString());
				page = userService.listUserWithPage(result, pageNum, pageSize);
				request.setAttribute("page", page);
				result.put("code", 1);
				target = "user/searchUser";
				log.info("展示客户信息数据成功");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		/** 添加客户信息 */
		else if("addUser".equals(toResult)){
			try {
				target = "user/addUser";
				result.put("code", 1);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		/** 修改客户信息 */
		else if("updateUser".equals(toResult)){
			try {
				target = "user/updateUser";
				result.put("id", param.get("supportID"));
				User user = userService.showUserById(result);
				request.setAttribute("user", user);
				result.put("code", 1);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		/** 删除客户信息 */
		else if("deleteUser".equals(toResult)){
			
		}
		/** 清零 */
		else if("clearBalance".equals(toResult)){
			
		}
		
		
		this.setParameters(param);
		this.setParameters(result);
		return target;
	}
	
	
	
	public TabUser loginUser() {
		TabUser loginUser = (TabUser) this.session.getAttribute(Constant.SESSION_LOGIN_USER);
		return loginUser;
	}
	
}
